#include "TeamsStatsMenu.h"
#include "Utilities.h"
#include "PrintUtilities.h"
#include "User.h"
#include "MenuManager.h"
#include "ServiceProvider.h"
#include "LeagueManager.h"
#include "GamePrintUtils.h"
#include "Team.h"
#include "TeamManager.h"
#include "TeamFunctors.h"

#include <algorithm>

TeamsStatsMenu::TeamsStatsMenu(MenuManager* const i_menuManager)
	: Menu(i_menuManager)
{
}

void TeamsStatsMenu::OnEnter()
{
	if(m_teams.empty())
		m_teams = GetSP()->GetTeamMgr().GetTeams();
}

void TeamsStatsMenu::Draw() const
{
	using namespace std;

	cout << Print::Title("TEAMS STATISTICS:");
	cout << " 1) Order by Names." << endl;
	cout << " 2) Order by Wins." << endl;
	cout << " 3) Order by Second Places." << endl;
	cout << " 4) Order by Third Places." << endl;
	cout << " 5) Order by Total Medal." << endl;
	cout << " 6) Order by Full Team Points." << endl;
	cout << " 7) Order by Recent Team Points." << endl;
	cout << endl << endl;
	
	ShowTeams();

	string options = "1-7) Select Sorting Criterion.\n\n0) Return to previous menu'.";
	cout << Print::Options(options) << "Selection: ";
}

void TeamsStatsMenu::ShowTeams() const
{
	using std::cout; using std::endl;
	const int MaxTeamNameSize = Team::GetParams().MAX_CHAR_TEAM_NAME;

	cout	<< Print::TabUpTo("Team Name", MaxTeamNameSize)
			<< Print::TabUpTo("W", 4)
			<< Print::TabUpTo("S", 4)
			<< Print::TabUpTo("T", 4)
			<< Print::TabUpTo("TOT", 4);

	const size_t layersNum = GetSP()->GetLeagueMgr().GetNumOfLayers(true);
	for(size_t i = 0; i < layersNum; ++i)
		cout << "LY" << i+1 << ' ';
	cout << "OUT ";

	cout << Print::TabUpTo("FTS", 6);
	cout << Print::TabUpTo("RTS", 5);
	cout << endl << endl;

	for (TeamVecCit it = m_teams.begin(); it != m_teams.end(); ++it)
	{
		const Team& theTeam = *(*it);
		const TeamHistory& theHistory = theTeam.GetTeamHistory();
		cout	<< Print::TabUpTo(theTeam.GetName(), MaxTeamNameSize)
				<< Print::TabUpTo(theHistory.m_podium[0], 4)
				<< Print::TabUpTo(theHistory.m_podium[1], 4)
				<< Print::TabUpTo(theHistory.m_podium[2], 4)
				<< Print::TabUpTo(theHistory.m_totalPodium, 4);

		for(size_t i = 0; i < layersNum; ++i)
			cout << Print::TabUpTo(theHistory.m_yearsInLeagueLayer[i], 4);

		int outYears = 0;
		for(size_t j = layersNum; j < GetSP()->GetLeagueMgr().GetNumOfLayers(false); ++j)
			outYears += theHistory.m_yearsInLeagueLayer[j];
		cout << Print::TabUpTo(outYears, 4);
		
		cout	<< Print::TabUpTo(theTeam.GetTeamPoints(0), 6)
				<< Print::TabUpTo(theTeam.GetTeamPoints(5), 5)
				<< endl;
	}
	cout << endl;
}

bool TeamsStatsMenu::Input(const std::string& i_input)
{
	bool goodInput = true;

	int res = Utilities::StringToInt(i_input);

	switch (res)
	{
		case 0:
			GetMM()->SetMenu(GetMM()->GetMainMenu());
			break;
		case 1:
			std::sort(m_teams.begin(), m_teams.end(), TeamComparisonName);
			break;
		case 2:
		case 3:
		case 4:
			std::sort(m_teams.begin(), m_teams.end(), TeamComparisonHistoryPodium(res-2));
			break;
		case 5:
			std::sort(m_teams.begin(), m_teams.end(), TeamComparisonHistoryTotalPodium);
			break;
		case 6:
			std::sort(m_teams.begin(), m_teams.end(), TeamComparisonTotalTeamPoints);
			break;
		case 7:
			std::sort(m_teams.begin(), m_teams.end(), TeamComparisonRecentTeamPoints);
			break;
		default:
			goodInput = false;
			break;
	}

	return goodInput;
}